package laschools.team1.Zelda;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class FrameMain {
//what is this int?
	private final static int LINK_MOVE_TIMEOUT = 8;
	
	private static FrameMain fm;
	
	private static double step;
	private static int squareLength;
	private static int HUDheight;
	private static int xScreen = 8, yScreen = 1;
	private static char[][] screen;
	private static String screenName;
	
	private static JFrame frame;
	private static HUDPanel hud;
	private static GamePanel screenPanel;
	
	private static boolean up, down, left, right;
	
	private static Link link;
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		loadSavedGame();
//you'll have to explain how you make a FrameMain inside FraimMain
//I don't understand
		fm = new FrameMain();
		step = 0;
		frame = new JFrame("Legend of Zelda");
		hud = new HUDPanel();
		screenPanel = new GamePanel();
		
		squareLength = calculateSquareLength();
		HUDheight = squareLength * 4;
		
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setLayout(new BorderLayout());
		
		frame.add(hud, BorderLayout.NORTH);
		frame.add(screenPanel, BorderLayout.CENTER);
		
		//frame.setSize(800, 776);
//are these dimension values significant?
		frame.setPreferredSize(new Dimension(800, 776));
		frame.pack();
		frame.setVisible(true);
		
		
		frame.addKeyListener(fm.new ZeldaListener());
		
		System.out.println(screenPanel.getWidth());
		
		//run();
		//System.out.println("Set to run once");
		while (link.getAlive()){
			step ++;
			run();
		}
	}
	
	/**
	 * This should load all the variables to correspond to a player's save state.
	 * Things I can think of right now:
	 * Position, screen, items, hearts
	 * It should read out of a text file (or something. An encoded image would be sweet).
	 */
	private static void loadSavedGame(){
		xScreen = 8;
		yScreen = 1;
		link = new Link((double)8, (double)7);
		
	}
	
	private static int calculateSquareLength(){
		int width, height;
		width = Math.round((float)(frame.getContentPane().getWidth() / (double) 16));
		height = Math.round((float)(((0.75 * frame.getContentPane().getHeight()) / 11)));
		return (width < height)? width : height;
	}

	private static void run(){
		squareLength = calculateSquareLength();
		HUDheight = squareLength * 4;
		
		try{
//wtf is "../../../" ???
			screenName = (fm.getClass().getResource("../../../Screens/" + xScreen + "-" + yScreen + ".txt").getPath());
		} catch (NullPointerException e){
			System.out.println("Yer' off the map matey!");
		}
		
		//System.out.println(screenName);
		screen = ScreenScanner.scanScreen(screenName);
		//System.out.println(squareLength);
		
		frame.repaint();
		hud.repaint();
		screenPanel.repaint();
		if (step % LINK_MOVE_TIMEOUT == 0){
			try {
				useKeys();
			} catch (ArrayIndexOutOfBoundsException e){
				System.out.println("Out of bounds");
				System.out.println(Link.getPosition());
			}
		}
		
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private static void useKeys() {
		if (up){
			link.keyUp();
		} else if (down){
			link.keyDown();
		} else if (left){
			link.keyLeft();
		} else if(right){
			link.keyRight();
		}
		//Other keys SHOULD NOT be else if 's
	}

	private class ZeldaListener implements KeyListener{

		@Override
		public void keyPressed(KeyEvent e) {
			int KeyCode = e.getKeyCode();
			
			if (KeyCode == KeyEvent.VK_UP){
				setAllDirectionsFalse();
				up = true;
			}
			if (KeyCode == KeyEvent.VK_DOWN){
				setAllDirectionsFalse();
				down = true;
			}
			if (KeyCode == KeyEvent.VK_LEFT){
				setAllDirectionsFalse();
				left = true;
			}
			if (KeyCode == KeyEvent.VK_RIGHT){
				setAllDirectionsFalse();
				right = true;
			}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			int KeyCode = e.getKeyCode();
			if (KeyCode == KeyEvent.VK_UP){
				up = false;
			}
			if (KeyCode == KeyEvent.VK_DOWN){
				down = false;
			}
			if (KeyCode == KeyEvent.VK_LEFT){
				left = false;
			}
			if (KeyCode == KeyEvent.VK_RIGHT){
				right = false;
			}
		}
		
		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private static void setAllDirectionsFalse(){
		up = false;
		down = false;
		left = false;
		right = false;
	}
	
	public static int getSquareLength() {
		return squareLength;
	}
	
	public static char[][] getScreen(){
		return screen;
	}
	
	public static int getHUDheight() {
		return HUDheight;
	}
	
	public static JFrame getObserver(){
		return frame;
	}
	
	/**
	 * Tells the game to change what screen we're in
	 * @param direction An int value 1,2,3, or 4 which tells the screen which way we need to go. 1 is up, 2 is right, 3 is down, 4 is left
	 */
	public static void changeScreen(int direction){
		switch (direction){
		case 1:
			yScreen ++;
			break;
		case 2:
			xScreen ++;
			break;
		case 3:
			yScreen --;
			break;
		case 4:
			xScreen --;
			break;
		default:
			System.err.println("Invalid changeScreen direction");
			break;
		}	
	}
	
	public static Point getScreenPosition(){
		return new Point(xScreen, yScreen);
	}
}
